package com.uchiha.nova.core.tool.util;

import com.uchiha.nova.core.tool.function.CheckedConsumer;
import com.uchiha.nova.core.tool.function.CheckedFunction;
import com.uchiha.nova.core.tool.function.CheckedRunnable;
import com.uchiha.nova.core.tool.function.CheckedSupplier;

import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @author 周林静
 * @Date 2021/4/29 16:00
 * @Version 1.0
 */
public class Unchecked {

    public static <T, R> Function<T, R> function(CheckedFunction<T, R> mapper) {
        Objects.requireNonNull(mapper);
        return t -> {
            try {
                return mapper.apply(t);
            } catch (Throwable e) {
                throw Exceptions.unchecked(e);
            }
        };
    }

    public static <T> Consumer<T> consumer(CheckedConsumer<T> mapper) {
        Objects.requireNonNull(mapper);
        return t -> {
            try {
                mapper.accept(t);
            } catch (Throwable e) {
                throw Exceptions.unchecked(e);
            }
        };
    }

    public static <T> Supplier<T> supplier(CheckedSupplier<T> mapper) {
        Objects.requireNonNull(mapper);
        return () -> {
            try {
                return mapper.get();
            } catch (Throwable e) {
                throw Exceptions.unchecked(e);
            }
        };
    }

    public static Runnable runnable(CheckedRunnable runnable) {
        Objects.requireNonNull(runnable);
        return () -> {
            try {
                runnable.run();
            } catch (Throwable e) {
                throw Exceptions.unchecked(e);
            }
        };
    }



}

